import axios from 'axios'

//获取微信code
function getWXcode($this){
    axios({
        url: "/test/index/Login/getwxcode",
        method: "get",
        params: {

        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            location.href=res.data.data.url;
        }else{
            // alert(res.data.message)
        }
    })
    .catch(function(res) {});
}
//获取验证码
function getCode($this){
    axios({
        url: "/test/index.php/index/Login/getCode",
        method: "get",
        params: {
            tel:$this.tel,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.iscode=false;
            var codeTime=setInterval(function(){
                $this.settime--;
                if($this.settime<=0){
                    clearInterval(codeTime)
                    $this.iscode=true;
                    $this.settime=60;
                }
            },1000)
        }else{
            alert(res.data.message);
        }
    })
    .catch(function(res) {});
}
//手机号登陆
function getCodeLogin($this){
    axios({
        url: "/test/index/login/codeLogin",
        method: "get",
        params: {
            tel:$this.tel,
            code:$this.code,
            openid:$this.appid,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            // location.href="./#/home"
            document.getElementById('toHome').click();
            $this.$cookie.set('token',res.data.data.user_token.token,7);
            

        }else{
            $this.$cookie.set('token',0,-1);
            
            alert(res.data.message);
        }
    })
    .catch(function(res) {});
}
//首页数据
function getDetailed($this,loading){
    axios({
        url: "/test/index/Spread/detailed",
        method: "get",
        params: {
            page:$this.page,
            token:$this.token,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data=res.data.data;
            $this.codeUrl=res.data.data.qecode;
            console.log($this.loading)
            loading.close();
            
        }else{
            // alert(res.data.message);
            $this.$cookie.set('token','',-1);
            loading.close(); 
            document.getElementById('toLogin').click();
                  
        }
    })
    .catch(function(res) {});
}
//获取二维码链接
function getQecode($this){
    axios({
        url: "/test/index/Spread/qecode",
        method: "get",
        params: {
            token:$this.token,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.codeUrl=res.data.data.url;
        }else{
            // alert(res.data.message)
            $this.$cookie.set('token',0,-1);
        }
    })
    .catch(function(res) {});
}
//我的客户api
function getCustomer($this,loading){
    axios({
        url: "/test/index/User/customer",
        method: "get",
        params: {
            token:$this.token,
            is_effective:$this.is_effective,
            months:$this.year+''+$this.month,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data=res.data.data;
            loading.close();
        }else{
            loading.close(); 
            $this.$cookie.set('token','',-1);
            document.getElementById('toLogin').click();
            // alert(res.data.message)
        }
    })
    .catch(function(res) {});
}
//个人业绩api
function getAchievement($this,loading){
    axios({
        url: "/test/index/User/achievement",
        method: "get",
        params: {
            token:$this.token,
            months:$this.year+''+$this.month,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data=res.data.data;
            loading.close();
        }else{
            loading.close(); 
            $this.$cookie.set('token','',-1);
            document.getElementById('toLogin').click();
            // alert(res.data.message)
        }
    })
    .catch(function(res) {});
}
//业绩排行榜
function getRankings($this,loading){
    axios({
        url: "/test/index/Spread/ranKings",
        method: "get",
        params: {
            token:$this.token,
            month:$this.year+''+$this.month,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data1=res.data.data;
            loading.close();
        }else{
            loading.close(); 
            $this.$cookie.set('token','',-1);
            document.getElementById('toLogin').click();
            // alert(res.data.message)
        }
    })
    .catch(function(res) {});
}
//注册用户排行榜
function getUsersranking($this,loading){
    axios({
        url: "/test/index/spread/usersranking",
        method: "get",
        params: {
            token:$this.token,
            month:$this.year+''+$this.month,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data2=res.data.data;
            loading.close();
        }else{
            loading.close(); 
            $this.$cookie.set('token','',-1);
            document.getElementById('toLogin').click();
            // alert(res.data.message)
        }
    })
    .catch(function(res) {});
}
//有效用户排行榜
function getUserRanking($this,loading){
    axios({
        url: "/test/index/Spread/userRanking",
        method: "get",
        params: {
            token:$this.token,
            month:$this.year+''+$this.month,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data3=res.data.data;
            loading.close();
        }else{
            loading.close(); 
            $this.$cookie.set('token','',-1);
            document.getElementById('toLogin').click();
            // alert(res.data.message)
        }
    })
    .catch(function(res) {});
}
//提成总金额
function getExtract($this,loading){
    axios({
        url: "/test/index/Spread/extract",
        method: "get",
        params: {
            token:$this.token,
            // month:$this.year+''+$this.month,
        }
      })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data=res.data.data;
            loading.close();
        }else{
            loading.close(); 
            $this.$cookie.set('token','',-1);
            document.getElementById('toLogin').click();
            // alert(res.data.message)
        }
    })
    .catch(function(res) {});
}
//申请提现
function setPutforward($this,loading){
    axios({
        url: "/test/index/Spread/putforward",
        method: "get",
        params: {
            token:$this.token,
            // month:$this.year+''+$this.month,
            name:$this.name,
            username:$this.username,
            number:$this.number,
            money:$this.money,
            type:$this.type,
        }
      })
    .then(function(res) {
        $this.isPop=true;
        var timer=setTimeout(function(){
            $this.isPop=false;
            clearInterval(timer);
            // location.reload();
        },3000)
        $this.isPost=res.data.message;
        $this.code=res.data.code;
        loading.close();
        if(res.data.code == 1){

        }else if(res.data.code == -2){
            $this.$cookie.set('token','',-1);
            loading.close();
            document.getElementById('toLogin').click();
        }
    })
    .catch(function(res) {});
}
//获取提现排列表
function getPutforward($this,loading){
    if($this.status==2){
        status=-1;
    }else{
        status=$this.status;
    }
    axios({
        url: "/test/index/User/putForwards",
        method: "get",
        params: {
            token:$this.token,
            status:status,
        }
    })
    .then(function(res) {
        if (res.data.code == 1) {
            $this.data=res.data.data;
            loading.close();
        }else{
            alert(res.data.message);
            loading.close();
            $this.$cookie.set('token','',-1);
            document.getElementById('toLogin').click();
        }
    })
    .catch(function(res) {});
}
export default {
    getWXcode,
    getCode,
    getCodeLogin,
    getDetailed,
    getQecode,
    getCustomer,
    getAchievement,
    getRankings,
    getUsersranking,
    getUserRanking,
    getExtract,
    setPutforward,
    getPutforward
};